package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/">807. 保持城市天际线</a>
 */
public class Solution807 extends BaseSolution {

    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int len = grid.length;
        int[][] h = new int[len][2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                h[i][0] = Math.max(h[i][0], grid[i][j]);
                h[i][1] = Math.max(h[i][1], grid[j][i]);
            }
        }
        int sum = 0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                sum += Math.min(h[i][0], h[j][1]) - grid[i][j];
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Solution807 solution = new Solution807();
        System.out.println(solution.maxIncreaseKeepingSkyline(new int[][]{{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}}));
    }
}